package com.ii.client;

import com.ii.client.handler.NettyHander;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.Scanner;

public class NettyClient {

    public static void main(String[] args) throws Exception{
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new NettyHander());
            System.out.println("netty client start");
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 9000).sync();
            Channel channel = channelFuture.channel();
            System.out.println("========= " + channel.localAddress() + " ===========");
//            Scanner sc = new Scanner(System.in);
//            while (sc.hasNext()) {
//                String str = sc.nextLine();
//                channel.writeAndFlush(str);
//            }
//            for (int index = 0; index <100 ; index ++) {
//                channel.writeAndFlush("五哥，是真帅 \n");
//            }
            String text = "xin tiao";
            while (channel.isActive()) {
                Thread.sleep(10*1000);
                channel.writeAndFlush(text);
            }

            channelFuture.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
